package com.abocode.aichat.data;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * @author guanxf
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class MessageStanzaImpl<T>  implements MessageStanza{
    private String id;
    private T body;
    private ModuleType moduleType;
    private ModuleServiceType moduleServiceType;
    private Long time = System.currentTimeMillis() / 1000;
    public MessageStanzaImpl(String id, T body) {
        this.id = id;
        this.body = body;
    }

    public MessageStanzaImpl(String id, T body, ModuleType moduleType, ModuleServiceType moduleServiceType) {
        this.id = id;
        this.body = body;
        this.moduleType = moduleType;
        this.moduleServiceType = moduleServiceType;
    }

    @Override
    public ModuleType moduleType() {
        return moduleType;
    }

    @Override
    public ModuleServiceType moduleServiceType() {
        return moduleServiceType;
    }

    @Override
    public String id() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public T body() {
        return body;
    }

    public void setBody(T body) {
        this.body = body;
    }

    @Override
    public String name() {
        return moduleServiceType.name();
    }

    @Override
    public boolean validate() {
        if (id() == null || moduleServiceType == null || moduleType == null) {
            throw new NullPointerException("id | moduleType | moduleServiceType must not be null");
        }
        return true;
    }
    @Override
    public String token() {
        return null;
    }

    @Override
    public Entity findTo() {
        try {
            MessageCommand obj= JSON.parseObject(body().toString(),MessageCommand.class);
            if(obj==null){
                return null;
            }
            Entity entity = EntityImpl.parse(String.valueOf(obj));
            return entity;
        } catch (Exception ex) {
            return null;
        }

    }
}

